﻿'Adam DuBois
'SD&D Fall 2009
'Team CompareThis
'User Interface

'System Packages required for UI
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports Microsoft.Win32
Imports System.Exception
Imports System.IO
Imports System.Text
Imports System.Diagnostics.Process

Public Class frmCompare


    'Closes Program
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Close()
    End Sub

    'Function Handles obtaining the directory path and managing previous paths used by deleting duplicates.
    Private Sub btnBrowse1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse1.Click
        Dim i As Integer, j As Integer, h As Boolean = False
        If rdbMultiple.Checked() Then 'If comparing folders the Browse folder dialog box runs
            If fbdSearch.ShowDialog() = Windows.Forms.DialogResult.OK Then
                cmbPath1.Text = fbdSearch.SelectedPath()
                cmbPath1.Items.Add(cmbPath1.Text)
                If cmbPath1.Items.Count() > 1 Then
                    j = cmbPath1.Items.Count() - 1
                    For i = 1 To j
                        If h = False Then
                            If cmbPath1.Text.Equals(cmbPath1.Items.Item(i - 1)) Then 'Check for duplicate paths
                                cmbPath1.Items.RemoveAt(i - 1)
                                cmbPath1.Text = fbdSearch.SelectedPath()
                                h = True
                            End If
                        End If
                    Next
                End If
            End If
        ElseIf rdbSingle.Checked() Then 'If comparing a file the Open File Dialog box runs
            If ofdSearch.ShowDialog() = Windows.Forms.DialogResult.OK Then
                cmbPath1.Text = ofdSearch.FileName()
                cmbPath1.Items.Add(cmbPath1.Text)
                If cmbPath1.Items.Count() > 1 Then
                    j = cmbPath1.Items.Count() - 1
                    For i = 1 To j
                        If h = False Then
                            If cmbPath1.Text.Equals(cmbPath1.Items.Item(i - 1)) Then 'Check for duplicate paths
                                cmbPath1.Items.RemoveAt(i - 1)
                                cmbPath1.Text = ofdSearch.FileName()
                                h = True
                            End If
                        End If
                    Next
                End If
            End If
        End If
    End Sub

    'Function Handles obtaining the directory path and managing previous paths used by deleting duplicates.
    Private Sub btnBrowse2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse2.Click
        Dim i As Integer, j As Integer, h As Boolean = False
        If rdbMultiple.Checked() Then  'If comparing folders the Browse folder dialog box runs
            If fbdSearch.ShowDialog() = Windows.Forms.DialogResult.OK Then
                cmbPath2.Text = fbdSearch.SelectedPath()
                cmbPath2.Items.Add(cmbPath2.Text)
                If cmbPath2.Items.Count() > 1 Then
                    j = cmbPath2.Items.Count() - 1
                    For i = 1 To j
                        If h = False Then
                            If cmbPath2.Text.Equals(cmbPath2.Items.Item(i - 1)) Then 'Check for duplicate paths
                                cmbPath2.Items.RemoveAt(i - 1)
                                cmbPath2.Text = fbdSearch.SelectedPath()
                                h = True
                            End If
                        End If
                    Next
                End If
            End If
        ElseIf rdbSingle.Checked() Then 'If comparing a file the Open File Dialog box runs
            If ofdSearch.ShowDialog() = Windows.Forms.DialogResult.OK Then
                cmbPath2.Text = ofdSearch.FileName()
                cmbPath2.Items.Add(cmbPath2.Text)
                If cmbPath2.Items.Count() > 1 Then
                    j = cmbPath2.Items.Count() - 1
                    For i = 1 To j
                        If h = False Then
                            If cmbPath2.Text.Equals(cmbPath2.Items.Item(i - 1)) Then 'Check for duplicate paths
                                cmbPath2.Items.RemoveAt(i - 1)
                                cmbPath2.Text = ofdSearch.FileName()
                                h = True
                            End If
                        End If
                    Next
                End If
            End If
        End If
    End Sub
    'Handles switch between selected combo button & clears Path text when changed.
    Private Sub rdbSingle_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbSingle.CheckedChanged
        If rdbSingle.Checked Then
            rdbMultiple.Checked = False
            cmbPath1.Text = ""
            cmbPath2.Text = ""
        End If
    End Sub
    'Handles switch between selected combo button & clears Path text when changed.
    Private Sub rdbMultiple_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbMultiple.CheckedChanged
        If rdbMultiple.Checked Then
            rdbSingle.Checked = False
            cmbPath1.Text = ""
            cmbPath2.Text = ""
        End If
    End Sub
    Private Sub frmCompare_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        clbDisplay.SetItemChecked(0, True)
        clbOutput.SetItemChecked(0, True)
    End Sub
    Private Sub frmCompare_FormClosed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.FormClosed

    End Sub
    'Function executes The Comparison
    '1st - Writes text to Config.txt to pass to compare.exe
    '2nd - Runs compare.exe
    '3rd - Deletes Config.txt
    Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click
        Dim arguments As String, opt1 As String, opt2 As String, opt3 As String
        Dim startinfo As New System.Diagnostics.ProcessStartInfo()

        arguments = ""
        opt1 = ""
        opt2 = ""
        opt3 = ""

        If clbOutput.GetItemChecked(0) And clbOutput.GetItemChecked(1) Then
            opt1 = "3" 'output to file & screen
        ElseIf clbOutput.GetItemChecked(1) Then
            opt1 = "2" 'output to screen
        Else
            opt1 = "1" 'output to file
        End If

        If clbDisplay.GetItemChecked(0) And clbDisplay.GetItemChecked(2) Then
            opt2 = "4" 'highlighting & percentage
        ElseIf clbDisplay.GetItemChecked(1) And clbDisplay.GetItemChecked(2) Then
            opt2 = "5" 'isolation & percentage
        ElseIf clbDisplay.GetItemChecked(2) Then
            opt2 = "3" 'percentage
        ElseIf clbDisplay.GetItemChecked(0) Then
            opt2 = "1" 'highlighting
        ElseIf clbDisplay.GetItemChecked(1) Then
            opt2 = "2" 'isolation
        End If

        'compare methods
        If cmbCompare.Text.Equals("Standard") Then
            opt3 = "1"
        ElseIf cmbCompare.Text.Equals("Numbers") Then
            opt3 = "2"
        ElseIf cmbCompare.Text.Equals("Non-alphanumeric") Then
            opt3 = "3"
        ElseIf cmbCompare.Text.Equals("White Space") Then
            opt3 = "4"
        End If

        arguments = cmbPath1.Text + " " + cmbPath2.Text + " " + opt1 + " " + opt2 + " " + opt3

        startinfo.WorkingDirectory() = My.Application.Info.DirectoryPath
        startinfo.FileName() = "compare.exe"
        startinfo.Arguments() = arguments

        Process.Start(startinfo)

        'Process.Start(My.Application.Info.DirectoryPath & "\compare.exe", cmbPath2.Text)
        'My.Computer.FileSystem.DeleteFile(My.Application.Info.DirectoryPath & "\config.txt")
    End Sub
End Class
